package com.lanchewuyou.user.driver.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.lanchewuyou.user.driver.lipeiui.PickCarActivity;
import com.lanchewuyou.user.driver.lipeiui.ReturnCarActivity;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.LocationApplication;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.squareup.okhttp.Request;

import java.io.IOException;

/**
 * 上传路径信息service.定时循环handler上传,service被销毁时移除对应任务
 */
public class DaohangService extends Service {
    private LocationClient mLocationClient;
    private BDLocation location;
    private String url;
    private String id;
    private String driverid;

    //循环执行代码
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //需要执行的代码
                    location = ((LocationApplication) getApplication()).personLocation;
                    StringBuffer sb = new StringBuffer();
                    sb.append(location.getLocationDescribe());
                    sb.append("\n");
                    sb.append(location.getLatitude());
                    sb.append("\n");
                    sb.append(location.getLongitude());
                    sb.append("\n");
                    sb.append(location.getTime());
                    Log.d("service", sb.toString());
                    OkHttpClientManager.postAsyn(url, new OkHttpClientManager.StringCallback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            Log.d("startdaohangacit", "errpr");
                        }

                        @Override
                        public void onResponse(String response) {
                            Log.d("startdaohangacit", response);

                        }
                    }, new OkHttpClientManager.Param[]{
                            new OkHttpClientManager.Param("orderid", id),
                            new OkHttpClientManager.Param("driverid", driverid),
                            new OkHttpClientManager.Param("lotype", 2 + ""),
                            new OkHttpClientManager.Param("longitute", location.getLongitude() + ""),
                            new OkHttpClientManager.Param("latitude", location.getLatitude() + "")});

                    Message message = handler.obtainMessage(1);
                    handler.sendMessageDelayed(message, 10000);//上传间隔时间改为10秒一次
            }
        }
    };

    public DaohangService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String from = intent.getStringExtra("from");
        if (from.equals("pickcar")) {
            id = PickCarActivity.orderId;
            url = Config.PICKCARDAOHANG;
        } else if (from.equals("returncar")) {
            id = ReturnCarActivity.ORDERID;
            url = Config.RETURNCARDAOHANG;
        }
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        driverid = sharedPreferences.getString("driverid", "");
        mLocationClient = ((LocationApplication) getApplication()).mLocationClient;
        initLocation();
        mLocationClient.start();//开启定位
        Message message = handler.obtainMessage(1);
        handler.sendMessageDelayed(message, 5000);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public void onDestroy() {
        Log.d("service", "destory");
        handler.removeMessages(1);
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("gcj02");//可选，默认gcj02，设置返回的定位结果坐标系，
        option.setScanSpan(10000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mLocationClient.setLocOption(option);
    }
}
